package com.aliyun.oss.userController;

import com.aliyun.oss.UserService.ShoppingCartService;
import com.aliyun.oss.pojo.DTO.ShoppingCartDTO;
import com.aliyun.oss.pojo.Entity.ShoppingCart;
import com.aliyun.oss.pojo.Vo.ResultVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("/user/shoppingCart")
@RestController
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;
    @PostMapping("/add")
    public ResultVO add(@RequestAttribute("id")Integer id, @RequestBody ShoppingCartDTO shoppingCartDTO){
        shoppingCartService.add(id,shoppingCartDTO);
        return ResultVO.success();
    }
    @GetMapping("/list")
    public ResultVO list(@RequestAttribute("id")Integer id){
        return ResultVO.success(shoppingCartService.list(id));
    }
//    减少菜品数量
    @PostMapping("/changeNumber")
    public ResultVO reduce(@RequestAttribute("id")Integer id, @RequestBody Map<String,Object> map){
        Integer number = (Integer) map.get("number");
        Integer shoppingCartId = (Integer) map.get("id");
        shoppingCartService.reduce(id,shoppingCartId,number);
        return ResultVO.success();
    }
//  根据id删除购物车中的菜品
    @DeleteMapping("/delete")
    public ResultVO delete(@RequestAttribute("id")Integer id,@RequestBody Map<String,Object> map){
        Integer shoppingCartId = (Integer) map.get("id");
        shoppingCartService.removeById(shoppingCartId);
        return ResultVO.success();
    }
//    根据用户id清空购物车
    @DeleteMapping("/clear")
    public ResultVO clear(@RequestAttribute("id")Integer id){
        QueryWrapper<ShoppingCart> eq = new QueryWrapper<ShoppingCart>().eq("user_id", id);
        shoppingCartService.remove(eq);
        return ResultVO.success();
    }
}
